var controller = {
	/**
	 * 初始化
	 */
	init : function() {
		var _this = this;
		_this.bindClick();
	},
	/**
	 * 绑定事件
	 */
	bindEvent : function() {
		var _this = this;
		_this.bindClick()
	},
	bindClick : function() {

		$(".authority_selected").bind("click", function() {
			// 资源属性
			var role_id = $(this).val();
			$("#editAuthority").val(role_id);
			roleAjax.loadAuthority(role_id);
			$("#addAuthorityDialog").modal("show");
		});

		$("#editAuthority").bind("click", function() {
			$("#addAuthorityDialog").modal("hide");
			// 资源属性
			var role_id = $(this).val();
			var roleAuthoritys = new Array();
			var i = 0;
			$("input[name='authority_enabled']:checked").each(function() {
				var roleAuthority = {
					role_id : role_id,
					authority_id : $(this).val(),
					enabled : 1
				}
				roleAuthoritys[i] = roleAuthority;
				i = i + 1;
			});
			if (roleAuthoritys.length == 0) {
				App.myDialog("请至少选择一个权限！", "提示");
				return;
			}
			debugger;
			var roleAuthorityString = JSON.stringify(roleAuthoritys);
			ajax.addOrUpdate("/authority/role/edit", roleAuthorityString, "更新权限信息成功！", "更新权限信息失败！");
		});

		$("#addDialogBtn").bind("click", function() {
			$("#editRoleBtn").addClass("hide");
			$("#addRoleBtn").removeClass("hide");
			$("#addDialog").modal('show');
		});

		$("#addRoleBtn").bind("click", function() {
			// 资源属性
			var role_name = $("#roleName").val();
			var role_desc = $("#roleDesc").val();
			var enabled = $("input[name='enabled']:checked").val();
			var issys = $("input[name='issys']:checked").val();
			if (enabled == 'on') {
				enabled = 1;
			} else {
				enabled = 0;
			}
			if (issys == 'on') {
				issys = 1;
			} else {
				issys = 0;
			}

			var role = {
				role_name : role_name,
				role_desc : role_desc,
				enabled : enabled,
				issys : issys
			}
			$("#addDialog").modal('hide');
			var roleString = JSON.stringify(role);
			ajax.addOrUpdate("/role/add", roleString, "添加角色信息成功！", "添加角色信息失败！");
		});

		$(".remove_role").bind("click", function() {
			// 资源属性
			var roleId = $(this).val();
			ajax.delete("/role/delete?roleId=" + roleId);
		});

		$(".edit_role").bind("click", function() {
			// 资源属性
			var role_id = $(this).val();
			$("#editRoleBtn").removeClass("hide");
			$("#addRoleBtn").addClass("hide");
			var role = ajax.load("/role?roleId=" + role_id, "获取角色详情信息失败！");
			$("#roleId").val(role.role_id);
			$("#roleName").val(role.role_name);
			$("#roleDesc").val(role.role_desc);
			if(role.enabled == 1) {
				$("#enabled").parent().attr("aria-checked","true");
				$("#enabled").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			if(role.issys == 1) {
				$("#issys").parent().attr("aria-checked","true");
				$("#issys").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			//###################
			$("#addDialog").modal('show');
		});

		$("#editRoleBtn").bind("click", function() {
			// 资源属性
			var role_id = $("#roleId").val();
			var role_name = $("#roleName").val();
			var role_desc = $("#roleDesc").val();
			var enabled = $("input[name='enabled']:checked").val();
			var issys = $("input[name='issys']:checked").val();
			if (enabled == 'on') {
				enabled = 1;
			} else {
				enabled = 0;
			}
			if (issys == 'on') {
				issys = 1;
			} else {
				issys = 0;
			}

			var role = {
				role_id : role_id,
				role_name : role_name,
				role_desc : role_desc,
				enabled : enabled,
				issys : issys
			}
			$("#addDialog").modal('hide');
			var roleString = JSON.stringify(role);
			ajax.addOrUpdate("/role/edit", roleString, "修改角色信息成功！", "修改角色信息失败！");
		});
	}
};
var roleAjax = {
	loadAuthority : function(data) {
		$
				.ajax({
					url : "/authority/role/list?roleId=" + data,
					type : "GET",
					async : false,
					data : data,
					dataType : "json",
					contentType : 'application/json;charset=utf-8',
					success : function(rs) {
						debugger
						if (rs.success) {
							$("#menu_table").html("");
							$("#menu_table")
									.append(
											"<tr><th style='width: 60px'>操作</th><th>权限名称</th><th>权限描述</th></tr>");
							for (var i = 0; i < rs.result.length; i++) {
								var trString = "<tr><td><label><input name='authority_enabled' type='checkbox' value='"
										+ rs.result[i].authority_id + "'";
								if (rs.result[i].ifInuse) {
									trString = trString + " checked";

								}
								trString = trString + "></label></td><td>"
										+ rs.result[i].authority_name
										+ "</td><td>"
										+ rs.result[i].authority_desc
										+ "</td></tr>"

								$("#menu_table").append(trString);
							}
							$("input[type='checkbox']").iCheck({
								checkboxClass : 'icheckbox_minimal'
							});
						} else {
							App.myDialog("添加权限失败：" + rs.result, "提示");
						}
					}
				});
	}
};
$(function() {
	controller.init();
});